<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>

<script>
  var deleteOrderItem = false;
  var deleteOrderItemid = 0;

  //删除某产品对应的订单项
  $(function () {
    $("a.deleteOrderItem").click(function () {
      deleteOrderItem = false;
      var oiid = $(this).attr("oiid")
      deleteOrderItemid = oiid;
      $("#deleteConfirmModal").modal('show');
    });

    //确认删除模态框的确认
    $("button.deleteConfirmButton").click(function () {
      deleteOrderItem = true;
      $("#deleteConfirmModal").modal('hide');
    });

    //触发模态框的退出时的函数
    $('#deleteConfirmModal').on('hidden.bs.modal', function (e) {
      if (deleteOrderItem) {
        var page = "/foredeleteOrderItem";
        $.post(
            page,
            {"oiid": deleteOrderItemid},
            function (result) {
              if ("success" == result) {
                $("tr.cartProductItemTR[oiid=" + deleteOrderItemid + "]").remove();
                //同步所有状态
                syncSelect();
                syncCreateOrderButton();
                calcCartSumPriceAndNumber();
              }
              else {
                location.href = "/front/login.jsp";
              }
            }
        );
      }
    })

    //单击框修改关于购物车中的产品是否选中
    $("img.cartProductItemIfSelected").click(function () {
      var selectit = $(this).attr("selectit")
      if ("selectit" == selectit) {//已经选中的改为没选中
        $(this).attr("src", "../../img/site/cartNotSelected.png");
        $(this).attr("selectit", "false")
        $(this).parents("tr.cartProductItemTR").css("background-color", "#fff");
      }
      else {
        $(this).attr("src", "../../img/site/cartSelected.png");
        $(this).attr("selectit", "selectit")
        $(this).parents("tr.cartProductItemTR").css("background-color", "#fff8e1");
      }
      syncSelect();
      syncCreateOrderButton();
      calcCartSumPriceAndNumber();
    });

    //单机全选框操作
    $("img.selectAllItem").click(function () {
      var selectit = $(this).attr("selectit")
      if ("selectit" == selectit) {//全选状态下点击，全不选
        $("img.selectAllItem").attr("src", "../../img/site/cartNotSelected.png");
        $("img.selectAllItem").attr("selectit", "false")
        $(".cartProductItemIfSelected").each(function () {
          $(this).attr("src", "../../img/site/cartNotSelected.png");
          $(this).attr("selectit", "false");
          $(this).parents("tr.cartProductItemTR").css("background-color", "#fff");
        });
      }
      else {
        $("img.selectAllItem").attr("src", "../../img/site/cartSelected.png");
        $("img.selectAllItem").attr("selectit", "selectit")
        $(".cartProductItemIfSelected").each(function () {
          $(this).attr("src", "../../img/site/cartSelected.png");
          $(this).attr("selectit", "selectit");
          $(this).parents("tr.cartProductItemTR").css("background-color", "#FFF8E1");
        });
      }
      syncCreateOrderButton();
      calcCartSumPriceAndNumber();
    });

    //设置某产品项数量
    $(".orderItemNumberSetting").keyup(function () {
      var pid = $(this).attr("pid");
      var stock = $("span.orderItemStock[pid=" + pid + "]").text();
      var price = $("span.orderItemPromotePrice[pid=" + pid + "]").text();

      var num = $(".orderItemNumberSetting[pid=" + pid + "]").val();
      num = parseInt(num);
      if (isNaN(num))
        num = 1;
      if (num <= 0)
        num = 1;
      if (num > stock)
        num = stock;

      syncPrice(pid, num, price);
    });

    //添加某项产品的数量
    $(".numberPlus").click(function () {
      var pid = $(this).attr("pid");
      var stock = $("span.orderItemStock[pid=" + pid + "]").text();
      var price = $("span.orderItemPromotePrice[pid=" + pid + "]").text();
      var num = $(".orderItemNumberSetting[pid=" + pid + "]").val();
      num++;
      if (num > stock)
        num = stock;
      syncPrice(pid, num, price);
    });

    //减少某项产品的数量
    $(".numberMinus").click(function () {
      var pid = $(this).attr("pid");
      var stock = $("span.orderItemStock[pid=" + pid + "]").text();
      var price = $("span.orderItemPromotePrice[pid=" + pid + "]").text();

      var num = $(".orderItemNumberSetting[pid=" + pid + "]").val();
      --num;
      if (num <= 0)
        num = 1;
      syncPrice(pid, num, price);
    });

    $("button.createOrderButton").click(function () {
      var params = "";
      $(".cartProductItemIfSelected").each(function () {
        if ("selectit" == $(this).attr("selectit")) {
          var oiid = $(this).attr("oiid");
          params += "&oiid=" + oiid;
        }
      });
      params = params.substring(1);
      location.href = "forebuyAll?" + params;
    });
  })

  //结算按钮操作，如果产品项都没选中就不可点击，同步结算按钮
  function syncCreateOrderButton() {
    var selectAny = false;
    $(".cartProductItemIfSelected").each(function () {
      if ("selectit" == $(this).attr("selectit")) {
        selectAny = true;
      }
    });

    if (selectAny) {
      $("button.createOrderButton").css("background-color", "#C40000");
      $("button.createOrderButton").removeAttr("disabled");
    }
    else {
      $("button.createOrderButton").css("background-color", "#AAAAAA");
      $("button.createOrderButton").attr("disabled", "disabled");
    }

  }

  //如果有某项产品没有选中，全选框不选中，同步全选框
  function syncSelect() {
    var selectAll = true;
    $(".cartProductItemIfSelected").each(function () {
      if ("false" == $(this).attr("selectit")) {
        selectAll = false;
      }
    });

    if (selectAll)
      $("img.selectAllItem").attr("src", "../../img/site/cartSelected.png");
    else
      $("img.selectAllItem").attr("src", "../../img/site/cartNotSelected.png");

  }

  //计算每一项产品的金额
  function calcCartSumPriceAndNumber() {
    var sum = 0;
    var totalNumber = 0;
    $("img.cartProductItemIfSelected[selectit='selectit']").each(function () {
      var oiid = $(this).attr("oiid");
      var price = $(".cartProductItemSmallSumPrice[oiid=" + oiid + "]").text();
      price = price.replace(/,/g, "");
      price = price.replace(/￥/g, "");
      sum += new Number(price);

      var num = $(".orderItemNumberSetting[oiid=" + oiid + "]").val();
      totalNumber += new Number(num);
    });

    $("span.cartSumPrice").html("￥" + formatMoney(sum));
    $("span.cartTitlePrice").html("￥" + formatMoney(sum));
    $("span.cartSumNumber").html(totalNumber);
  }

  function syncPrice(pid, num, price) {
    $(".orderItemNumberSetting[pid=" + pid + "]").val(num);
    var cartProductItemSmallSumPrice = formatMoney(num * price);
    $(".cartProductItemSmallSumPrice[pid=" + pid + "]").html("￥" + cartProductItemSmallSumPrice);
    calcCartSumPriceAndNumber();

    var page = "/forechangeOrderItem";
    $.post(
        page,
        {"pid": pid, "number": num},
        function (result) {
          if ("success" != result) {
            location.href = "/front/login.jsp";
          }
        }
    );

  }
</script>

<title>购物车</title>
<div class="cartDiv">
  <div class="cartTitle pull-right">
    <span>已选商品  (不含运费)</span>
    <span class="cartTitlePrice">￥0.00</span>
    <button class="createOrderButton" disabled="disabled">结 算</button>
  </div>

  <div class="cartProductList">
    <table class="cartProductTable">
      <thead>
      <tr>
        <th class="selectAndImage">
          <img selectit="false" class="selectAllItem" src="../../img/site/cartNotSelected.png">全选
        </th>
        <th>商品信息</th>
        <th>单价</th>
        <th>数量</th>
        <th width="120px">金额</th>
        <th class="operation">操作</th>
      </tr>
      </thead>
      <tbody>
      <c:forEach items="${oi_list}" var="oi">
        <tr oiid="${oi.id}" class="cartProductItemTR">
          <td>
            <img selectit="false" oiid="${oi.id}" class="cartProductItemIfSelected"
                 src="../../img/site/cartNotSelected.png">
            <a style="display:none" href="#nowhere"><img src="../../img/site/cartSelected.png"></a>
            <c:if test="${empty oi.product.firstProductImage}">
              <img class="cartProductImg" src="../../img/site/imgNotFound.jpg">
            </c:if>
            <c:if test="${!empty oi.product.firstProductImage}">
              <img class="cartProductImg" src="../../img/productSingle_middle/${oi.product.firstProductImage.id}.jpg">
            </c:if>
          </td>

          <td>
            <div class="cartProductLinkOutDiv">
              <a href="/foreproduct?pid=${oi.product.id}" class="cartProductLink">${oi.product.name}</a>
              <div class="cartProductLinkInnerDiv">
                <img src="../../img/site/creditcard.png" title="支持信用卡支付">
                <img src="../../img/site/7day.png" title="消费者保障服务,承诺7天退货">
                <img src="../../img/site/promise.png" title="消费者保障服务,承诺如实描述">
              </div>
            </div>
          </td>

          <td>
            <span class="cartProductItemOriginalPrice">￥${oi.product.originalPrice}</span>
            <span class="cartProductItemPromotionPrice">￥${oi.product.promotePrice}</span>
          </td>
          <td>
            <div class="cartProductChangeNumberDiv">
              <span class="hidden orderItemStock" pid="${oi.product.id}">${oi.product.stock}</span>
              <span class="hidden orderItemPromotePrice" pid="${oi.product.id}">${oi.product.promotePrice}</span>
              <a pid="${oi.product.id}" class="numberMinus" href="#nowhere">-</a>
              <input pid="${oi.product.id}" oiid="${oi.id}" class="orderItemNumberSetting" autocomplete="off"
                     value="${oi.number}">
              <a stock="${oi.product.stock}" pid="${oi.product.id}" class="numberPlus" href="#nowhere">+</a>
            </div>
          </td>
          <td width="120px">
            <span class="cartProductItemSmallSumPrice" oiid="${oi.id}" pid="${oi.product.id}">
            ￥<fmt:formatNumber type="number" value="${oi.product.promotePrice*oi.number}" minFractionDigits="2"/></span>
          </td>
          <td>
            <a class="deleteOrderItem" oiid="${oi.id}" href="#nowhere">删除</a>
          </td>
        </tr>
      </c:forEach>
      </tbody>
    </table>
  </div>

  <div class="cartFoot">
    <img selectit="false" class="selectAllItem" src="../../img/site/cartNotSelected.png">
    <span>全选</span>
    <!--<a href="#">删除</a>-->
    <div class="pull-right">
      <span>已选商品 <span class="cartSumNumber">0</span> 件</span>
      <span>合计 (不含运费): </span>
      <span class="cartSumPrice">￥0.00</span>
      <button class="createOrderButton" disabled="disabled">结 算</button>
    </div>
  </div>
</div>